# -*- coding: utf-8 -*-
# @Time    : 2017/5/10


from sympy import S

from mathsolver.functions.base import BaseFunction, BaseIneq, BaseEq, BaseVariable, BasePoly
from mathsolver.functions.budengshi.jiebudengshi import JieBuDengShi
from mathsolver.functions.hanshu.helper import check_func, check_inter
from mathsolver.functions.mathematica.mathematicaSolve import MathematicaSolve


class BiJiaoHanShu(BaseFunction):
    """函数比较大小"""

    def solver(self, *args):
        # 处理输入
        if isinstance(args[1], BasePoly):
            func1 = check_func(args[0])
            expr1, expr2, var = func1.expression, args[1].sympify(), func1.var
        else:
            func1, func2 = check_func(args[0]), check_func(args[1])
            expr1, expr2, var = func1.expression, func2.expression, func1.var

        if func1.dingyiyu:  # 区间可能放在 BaseFunc 的 dingyiyu 属性里面
            dingyi_interval = check_inter(func1.dingyiyu)
        else:  # 区间放在 known 里面
            dingyi_interval = self.search(var)
            if not dingyi_interval:
                dingyi_interval = S.Reals

        # 解不等式
        ineq1 = BaseIneq([expr1, ">", expr2])
        ineq1_solver = JieBuDengShi().solver(ineq1, BaseVariable(var), {var: dingyi_interval})
        self.steps.append(["", "当 %s 时，解得：%s" % (ineq1.printing(), ineq1_solver.output[0].printing())])

        ineq2 = BaseIneq([expr1, "<", expr2])
        ineq2_solver = JieBuDengShi().solver(ineq2, BaseVariable(var), {var: dingyi_interval})
        self.steps.append(["", "当 %s 时，解得：%s" % (ineq2.printing(), ineq2_solver.output[0].printing())])

        eq = BaseEq([expr1, expr2])
        eq_solver = MathematicaSolve().solver(eq, BaseVariable(var), {var: dingyi_interval})
        self.steps.append(["", "当 %s 时，解得：%s" % (eq.printing(), eq_solver.output[0].printing())])

        self.label.add("函数比较大小")
        return self


if __name__ == '__main__':
    pass
